package com.second.app.thread.线程基础.day01;

/**
 * @Author GYZ
 * @Date 2023/11/16
 * <pre>
 *  线程创建方式
 *  1.继承Thread类
 *  2.实现Runnable类
 *  3.证明线程在sleep时候不释放锁,
 *    线程C执行时候持有线程A对象
 *    线程B,先执行线程A,然后A线程内部睡眠5s,线程B比线程A多睡眠1s,线程B睡醒，释放锁线程A对象
 *    此时线程C继续执行。
 * </pre>
 */
public class Main {
    public static void main(String[] args) {
        class MyThread extends Thread{
            MyThread(String name){
                super(name);
            }
            @Override
            public void run() {
                try {
                    System.out.println("线程名称："+Thread.currentThread().getName()+"执行开始："+System.currentTimeMillis());
                    Thread.sleep(5000);
                    System.out.println("线程名称："+Thread.currentThread().getName()+"执行结束："+System.currentTimeMillis());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        MyThread threadA = new MyThread("线程名称A");

        Thread threadB = new Thread(() -> {
            synchronized (threadA) {
                try {
                    threadA.start();
                    Thread.sleep(6000);
                    System.out.println("线程名称："+Thread.currentThread().getName()+"执行结束："+System.currentTimeMillis());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "线程名称B");
        threadB.start();

        new Thread(() -> {
            synchronized (threadA){
                System.out.println("线程名称："+Thread.currentThread().getName()+"打印："+System.currentTimeMillis());
            }
        },"线程C").start();
    }
}
